package com.darren.framework.service.impl;

import com.darren.archive.component.SysParameterHelper;
import com.darren.archive.entity.SysUser;
import com.darren.framework.Exception.ArchiveException;
import com.darren.framework.controller.BaseController;
import com.darren.framework.dao.GenericDao;
import com.darren.framework.entity.BaseEntity;
import com.darren.framework.repository.GenericRepository;
import com.darren.framework.service.GenericService;
import com.darren.framework.utils.Constant;
import com.darren.framework.utils.EncryptAESUtil;
import org.aspectj.lang.annotation.DeclareWarning;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)//默认该类的所有方法都开启事务，遇到Exception就回滚
public abstract class GenericServiceImpl<T extends BaseEntity, PK extends Serializable> extends BaseServiceImpl implements GenericService<T, PK> {

    @Autowired
    protected GenericDao<T, PK> genericDao;
    @Override
    public T save(T entity) {
        entity = genericDao.merge(entity);
        return entity;
    }

    @Override
    public T update(T entity) {
        return genericDao.update(entity);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, readOnly = true)
    public T findById(PK id) {
        return genericDao.findById(id);
    }

    @Override
    public void delete(T entity) {
        genericDao.delete(entity);
    }

    @Override
    public void deleteById(PK id) {
        genericDao.deleteById(id);
    }

    @Override
    public List<T> getAll() {
        return genericDao.getAll();
    }

}
